<%@ page import="java.io.*" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.net.HttpURLConnection" %>
<%@ page import="java.net.URL" %>
<%@ page import="ApiConfig" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String requestedPath = request.getParameter("path");
    String fileId = request.getParameter("fileId");

    if ((requestedPath == null || requestedPath.trim().isEmpty()) && 
        (fileId == null || fileId.trim().isEmpty())) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        out.println("Missing 'path' or 'fileId' parameter.");
        return;
    }

    try {
        String downloadUrl;
        if (fileId != null && !fileId.trim().isEmpty()) {
            // 使用文件ID下载
            downloadUrl = ApiConfig.getDownloadUrl(fileId);
        } else {
            // 使用文件路径下载
            downloadUrl = ApiConfig.getDownloadByPathUrl(requestedPath, null);
        }

        // 创建HTTP连接
        URL url = new URL(downloadUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(30000);
        
        int responseCode = connection.getResponseCode();
        
        if (responseCode == HttpURLConnection.HTTP_OK) {
            // 获取文件名
            String contentDisposition = connection.getHeaderField("Content-Disposition");
            String fileName = "download";
            if (contentDisposition != null && contentDisposition.contains("filename=")) {
                fileName = contentDisposition.substring(contentDisposition.indexOf("filename=") + 9);
                if (fileName.startsWith("\"") && fileName.endsWith("\"")) {
                    fileName = fileName.substring(1, fileName.length() - 1);
                }
            }
            
            // 设置响应头
            response.reset();
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            
            // 获取内容长度
            String contentLength = connection.getHeaderField("Content-Length");
            if (contentLength != null) {
                response.setHeader("Content-Length", contentLength);
            }
            
            // 复制数据流
            InputStream inputStream = connection.getInputStream();
            OutputStream outputStream = response.getOutputStream();
            
            byte[] buffer = new byte[8192];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            
            outputStream.flush();
            inputStream.close();
            
        } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            out.println("File not found.");
        } else if (responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            out.println("Access denied.");
        } else {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.println("Server error: " + responseCode);
        }
        
        connection.disconnect();
        
    } catch (Exception e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        out.println("Error: " + e.getMessage());
        e.printStackTrace();
    }
%> 